package shm;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;

public class SimulatedSerialThread extends Thread{
	
	private final static int NO_OF_NODE = 3;
	
	
	public static void showTestButton(JFrame frame) {
		JButton btnDebug = new JButton("Debug");
	
		btnDebug.setBounds(300, 3, 80, 25);
		frame.getContentPane().add(btnDebug);

		btnDebug.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				 
				Thread t = new SimulatedSerialThread();
				t.start();
			}
		});
	}

	@Override
	public void run() {
		
		int sampleRate = 100;
		
		long timeStart = System.currentTimeMillis()/sampleRate;
		
		Random rand = new Random();
		
		while(true) {
			
			try {
				Thread.sleep(sampleRate);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			for(int i = 1; i <= NO_OF_NODE; i++) {
				
				int x = rand.nextInt(65530);
				int y = rand.nextInt(65530);
				int z = rand.nextInt(65530);
				int ext = rand.nextInt(65530);
				
				int tick = Integer.valueOf(String.valueOf(System.currentTimeMillis() / sampleRate - timeStart));
				
				MainUI.os.newData(i, tick, x, y, z, ext);	
			}
			
		}

	}
	

}
